package org.ly.supermarketsystem.dao.impl;

import org.ly.supermarketsystem.dao.IUserDao;
import org.ly.supermarketsystem.entity.User;
import org.ly.supermarketsystem.utils.DruidConnectionUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @Author: Ben
 * @Date: 2021/12/27 17:33
 */
public class UserDaoImpl implements IUserDao {

    DruidConnectionUtils druidConnectionUtils=new DruidConnectionUtils();
    @Override
    public boolean insertUser(Connection connection, User user) {
        String sql = "insert into tb_user (username,password) values(?,?)";
        PreparedStatement pstmt=null;
        try {
            pstmt = (PreparedStatement) connection.prepareStatement(sql);
            pstmt.setString(1, user.getUserName());
            pstmt.setString(2, user.getPassword());
            pstmt.executeUpdate();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }finally {
            druidConnectionUtils.closeConnection(connection,pstmt,null);
        }
    }
}
